/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.action;

import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts3Action;
import javacommon.util.ResInfo;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.ObjectUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.pub.model.PubData;
import com.tech15.pub.model.PubInfo;
import com.tech15.pub.service.PubDataManager;
import com.tech15.pub.service.PubInfoManager;
import com.tech15.pub.vo.query.PubInfoQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class PubInfoAction extends BaseStruts3Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "istop desc,fbsj desc"; 
	
	//forward paths
	
	protected static final String LIST_TZGG_JSP= "/pub/pubInfo_listTzgg.jsp";
	protected static final String LIST_GZZD_JSP= "/pub/pubInfo_listGzzd.jsp";
	protected static final String LIST_JSP= "/pub/pubInfo_list.jsp";
	protected static final String CREATE_JSP = "/pub/pubInfo_add.jsp";
	protected static final String EDIT_JSP = "/pub/pubInfo_edit.jsp";
	protected static final String SHOW_JSP = "/pub/pubInfo_show.jsp";
	protected static final String VIEW_JSP = "/pub/pubInfo_view.jsp";
	protected static final String EDIT_TOP = "/pub/pubInfo_edit_top.jsp";
	
	protected static final String CREATE_TZGG_JSP = "/pub/pubInfo_addTzgg.jsp";
	protected static final String EDIT_TZGG_JSP = "/pub/pubInfo_editTzgg.jsp";
	
	protected static final String CREATE_GZZD_JSP = "/pub/pubInfo_addGzzd.jsp";
	protected static final String EDIT_GZZD_JSP = "/pub/pubInfo_editGzzd.jsp";
	
	protected static final String SEARCH_TZGG_JSP = "/pub/pub_tzgg.jsp";
	protected static final String SEARCH_GZZD_JSP = "/pub/pub_gzzd.jsp";
	protected static final String DETAIL_JSP = "/pub/pub_info_detail.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pub/list.do";
	
	private PubInfoManager pubInfoManager;
	private PubDataManager pubDataManager;
	
	private PubInfo pubInfo;
	java.lang.String id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			pubInfo = new PubInfo();
		} else {
			pubInfo = (PubInfo)pubInfoManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setPubInfoManager(PubInfoManager manager) {
		this.pubInfoManager = manager;
	}	
	public void setPubDataManager(PubDataManager manager) {
		this.pubDataManager = manager;
	}
	
	public Object getModel() {
		return pubInfo;
	}
	
	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		PubInfoQuery query = newQuery(PubInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setZt(new Long(9));
		Page page = pubInfoManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	public String listTzgg() {
		PubInfoQuery query = newQuery(PubInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setZt(new Long(9));
		query.setFbr(getLoginInfo().getUserid());
		Page page = pubInfoManager.findPage(query);
		savePage(page,query);
		return LIST_TZGG_JSP;
	}
	
	public String listGzzd() {
		PubInfoQuery query = newQuery(PubInfoQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = pubInfoManager.findPageGzzd(query);
		savePage(page,query);
		return LIST_GZZD_JSP;
	}
	
	public String tzgg() {
		PubInfoQuery query = newQuery(PubInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setZt(new Long(9));
		query.setUserid(getLoginInfo().getUserid());
		Page page = pubInfoManager.findPage(query);
		savePage(page,query);
		
		List<PubData> lbList = pubDataManager.searchByPid(new Long(9));
		getRequest().setAttribute("lbList", lbList);
		return SEARCH_TZGG_JSP;
	}
	
	public String gzzd() {
		PubInfoQuery query = newQuery(PubInfoQuery.class,DEFAULT_SORT_COLUMNS);
		Page page = pubInfoManager.findPageGzzd(query);
		savePage(page,query);
		
		List<PubData> lbList = pubDataManager.searchByPid(new Long(58));
		getRequest().setAttribute("lbList", lbList);
		
		if(ObjectUtils.isNotEmpty(pubInfo.getZt())){
			List<PubData> sublbList = pubDataManager.searchByPid(pubInfo.getZt());
			getRequest().setAttribute("sublbList", sublbList);
		}else{
			List<PubData> sublbList = pubDataManager.searchByPids(new Long(58));
			getRequest().setAttribute("sublbList", sublbList);
		}
		return SEARCH_GZZD_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	public String detail() {
		return DETAIL_JSP;
	}
	
	public String view() {
		return VIEW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	public String createTzgg() {
		return CREATE_TZGG_JSP;
	}
	
	public String createGzzd() {
		return CREATE_GZZD_JSP;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			pubInfo.setFbr(this.getLoginInfo().getUserid());
			pubInfoManager.saveInfo(pubInfo,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	
	/**进入更新页面*/
	public String edit() {
		if(!this.isNullOrEmptyString(this.pubInfo.getZt())){
			List<PubData> lbList = pubDataManager.searchByPid(this.pubInfo.getZt());
			getRequest().setAttribute("lbList", lbList);
		}
		return EDIT_JSP;
	}
	
	public String editTzgg() {
		if(!this.isNullOrEmptyString(this.pubInfo.getZt())){
			List<PubData> lbList = pubDataManager.searchByPid(this.pubInfo.getZt());
			getRequest().setAttribute("lbList", lbList);
		}
		return EDIT_TZGG_JSP;
	}
	
	public String editGzzd() {
		if(!this.isNullOrEmptyString(this.pubInfo.getZt())){
			List<PubData> lbList = pubDataManager.searchByPid(this.pubInfo.getZt());
			getRequest().setAttribute("lbList", lbList);
		}
		return EDIT_GZZD_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			pubInfo.setFbr(this.getLoginInfo().getUserid());
			pubInfoManager.updateInfo(pubInfo,realpath,getAttachFiles(),getAttachFilesFileName(),getAttachFilesContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	public String editTop() {
		return EDIT_TOP;
	}
	
	public void updateTop() {
		Map<String, Object> resInfo = null;
		try{
			pubInfoManager.update(pubInfo);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			pubInfoManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
}
